package picol.model.asa.inst;

import java.util.ArrayList;
import java.util.List;
import picol.model.asa.expr.ASAExpr;

/**
 * Classe de gestion de l'instruction if
 * @author Aurélien Marcel - Julien Sanderson
 */
public class ASAInstIf extends ASAInst {

    private ASAExpr test;
    private List<ASAInst> alors;
    private List<ASAInst> sinon;

    public ASAInstIf() {
        this.alors = new ArrayList<ASAInst>();
        this.sinon = new ArrayList<ASAInst>();
    }

    public void addAlors(ASAInst _inst) {
        this.alors.add(_inst);
    }

    public void addSinon(ASAInst _inst) {
        this.sinon.add(_inst);
    }

    public void setTest(ASAExpr test) {
        this.test = test;
    }

    @Override
    public void executer() {
        int evalue = this.test.evalue();
        if (evalue != 0) {
            for (ASAInst inst : this.alors) {
                inst.executer();
            }

        } else {
            if (this.sinon != null) {
                for (ASAInst inst : this.sinon) {
                    inst.executer();
                }
            }
        }
    }

    @Override
    public boolean executerDebug() {
        int evalue = this.test.evalue();
        if (evalue != 0) {
            if (this.alors.iterator().hasNext()) {
                return this.alors.iterator().next().executerDebug();
            }

        } else {
            if (this.sinon != null) {
                if (this.sinon.iterator().hasNext()) {
                    return this.sinon.iterator().next().executerDebug();
                }
            }
        }
        return false;
    }

    @Override
    public int getNbInst() {
        int nbinst = 0;
        for (ASAInst inst : alors) {
            nbinst += inst.getNbInst();
            nbinst++;
        }
        for (ASAInst inst : sinon) {
            nbinst += inst.getNbInst();
            nbinst++;
        }
        return nbinst;
    }
}
